package com.snail.billing.data;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.text.TextUtils;

public class AccountArray {

	private List<Account> list;

	public AccountArray() {
		list = new ArrayList<Account>();
	}

	public AccountArray(String jsonString) {
		list = new ArrayList<Account>();
		if (TextUtils.isEmpty(jsonString)) {
			return;
		}
		try {
			JSONArray array = new JSONArray(jsonString);
			for (int i = 0; i < array.length(); i++) {
				Account account = new Account(array.getString(i));
				list.add(account);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public List<Account> getAll() {
		return list;
	}

	public int length() {
		return list.size();
	}

	public void add(Account account) {
		list.add(account);
	}

	public void addFirst(Account account) {
		list.add(0, account);
	}

	public void remove(Account account) {
		list.remove(account);
	}

	public void removeFirst() {
		list.remove(0);
	}

	public void removeLast() {
		list.remove(list.size() - 1);
	}

	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[");
		for (int i = 0; i < list.size(); i++) {
			if (i != 0) {
				builder.append(",");
			}
			builder.append(list.get(i).toString());
		}
		builder.append("]");
		return builder.toString();
	}
}
